////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.adapter.NewsListItemAdapter;
import cc.telecomdigital.tdstock.adapter.NewsViewHolder;

/**
 * 
 * @author haibo
 * 
 *  for list item of warrants 
 *
 */
public class WarrantsItemActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String		TAG			= WarrantsItemActivity.class.getSimpleName();
	private static final long			START_ID	= 90000l;

	private ListView					_listView;
	private Button						_toolBtnReturn;
	private TextView					_topBarTitle;

	private static NewsListItemAdapter	_newsAdapter;
	private static List<NewsViewHolder>	_newsList;
	private String[]					_newsTitleList;
	private String[]					_newsContentList;
	private String[]					_newsTitle;
	private String[]					_timeStamp;


	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_warrants);

		_topBarTitle = (TextView) findViewById(R.id.warrants_item);
		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

				Intent intent = new Intent(getApplicationContext(), WarrantsActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				SwitchForwardActivity(WarrantsActivity.class, intent);
			}
		});

		_messageHandler = null; //this activity no need it.
		_listView = (ListView) findViewById(R.id.more_warrants_list);

		_newsList = new ArrayList<NewsViewHolder>();
	}

	private void updateAdapter()
	{
		long id = START_ID + _newsList.size();
		for (int index = 0, listSize = _newsTitleList == null ? 0
				: _newsTitleList.length; index < listSize; index++)
		{
			id += index;
			//CommonViewHolder viewHolder = new CommonViewHolder();
			NewsViewHolder viewHolder = new NewsViewHolder();
			viewHolder.id = id;
			viewHolder.text = new TextView(this);
			viewHolder.text.setText(_newsTitleList[index]);
			viewHolder.text.setTextColor(Color.BLACK);
			viewHolder.sign = new ImageView(this);
			viewHolder.isPaging = false;
			_newsList.add(viewHolder);
		}
		// modify by zor(ouyangzhihui)  --start
		if (_newsList.size() == 0)
		{
			NewsViewHolder newsViewHolder = new NewsViewHolder();
			newsViewHolder.seq = new TextView(this);
			newsViewHolder.text = new TextView(this);
			newsViewHolder.sign = new ImageView(this);
			newsViewHolder.isPaging = false;
			newsViewHolder.id = -1;
			newsViewHolder.seq.setText("-1");
			newsViewHolder.text.setText(getResources().getString(
					R.string.provide_no_information));
			_newsList.add(newsViewHolder);
		}
		//modify by zor(ouyangzhihui)  --end

		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				if (position != -1 && id != -1)
				{
					String topBarTitle = ((ITDLApplication) getApplicationContext()).getString(CommonDefn.TOP_BAR_TITLE);
					
					WarrantsItemCommonContentActivity._title = topBarTitle;
					WarrantsItemCommonContentActivity._content = _newsContentList[position];
//					WarrantsItemCommonContentActivity._cls =  WarrantsItemActivity.class;
					WarrantsItemCommonContentActivity._timeStamp =  _timeStamp[position];
					WarrantsItemCommonContentActivity._newsTitle =  _newsTitle[position];
					
					Intent intent = new Intent(getApplicationContext(), WarrantsItemCommonContentActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
					SwitchForwardActivity(WarrantsItemCommonContentActivity.class, intent);
				}
			}
		});

		if (_listView.getAdapter() == null)
		{
			_newsAdapter = new NewsListItemAdapter(LayoutInflater.from(this), _newsList);
			_listView.setAdapter(_newsAdapter);
		}
		else
		{
			_newsAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		updateTitle();
		_listView.invalidateViews();
	}
	
	private void updateTitle()
	{
		String topBarTitle = ((ITDLApplication) getApplicationContext()).getString(CommonDefn.TOP_BAR_TITLE);
		_topBarTitle.setText(topBarTitle);
	}
	
	private void updateData(List<String[]> result)
	{
		try
		{
			_newsTitleList = new String[result.size()];
			_newsContentList = new String[result.size()];
	
			_timeStamp = new String[result.size()];
			_newsTitle = new String[result.size()];
			int index = 0;
			StringBuffer newsContent = new StringBuffer();
			for (String[] array : result)
			{
				_timeStamp[index] = array[4].length() >= 16 ? array[4].substring(0, 16) : array[4];
				_newsTitle[index] = array[2];
				newsContent.append(array[3]);
	
				_newsContentList[index] = newsContent.toString();
				_newsTitleList[index] = String.format("%s  %s ", array[4].substring(0, 16), array[2]);
				
				TDStockLog.d(TAG, "title=" + _newsTitleList[index]);
				
				index++;
				newsContent.setLength(0);
			}
			
			updateAdapter();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	static public int iTypeId = 0;
	
	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		if (!_newsList.isEmpty())
		{
			_newsList.clear();
			_newsAdapter.notifyDataSetChanged();
		}
		
		return HttpAppInfo.SendWarrantsRequest(actContext, iTypeId, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(final H_UserClient_HttpRequest_Result iResult)
	{
		if(!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError+"("+iResult.MStatus+")");
			return;
		}
		
		Runnable runnable = new Runnable()
		{
			@Override
			public void run()
			{
				updateData(iResult.contentStores);
			}
		};
		_messageHandler.post(runnable);
	}
	
}
